#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int max_val(vector<vector<int>> & d, int n, int m, vector<vector<int>> & rec)
{
        if(n == d.size() || m == d[0].size()) return 0;
        if(rec[n][m] != -1) return rec[n][m];
        //if(n == d.size() - 1 && m == d[0].size() - 1) return d.back().back();

        int maxv = d[n][m];
        int max_right = max_val(d, n + 1, m, rec);
        int max_down = max_val(d, n, m + 1, rec);
        return rec[n][m] = maxv + max(max_right, max_down);     

}

int max_dp(vector<vector<int>> &d)
{
        int n = d.size();
        int m = d[0].size();
        vector<vector<int>> dp(n, vector<int>(m, 0));
        dp[n - 1][m - 1] = d[n - 1][m - 1];
        for(int i = n - 1; i >= 0; i--){
                for(int j = m - 1; j >= 0; j--){
                        if(i < n - 1) dp[i][j] = max(dp[i][j], dp[i + 1][j]);
                        if(j < m - 1) dp[i][j] = max(dp[i][j], dp[i][j + 1]);
                        dp[i][j] += d[i][j];
                }
        }
        dp[0][0] = d[0][0];
        for(int i = 1; i < n; i++){
                dp[i][0] = dp[i -1][0] + d[i][0];
        }
        for(int i = 1; i < m; i++){
                dp[0][i] = dp[0][i - 1] + d[0][i];
        }

        for(int i = 1; i < n; i++){
                for(int j = 1; j < m; j++){
                        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + d[i][j];
                }
        }

        return dp.back().back();        
}

int max_dp_space(vector<vector<int>> &d)
{
        int n = d.size();
        int m = d[0].size();

        vector<int> dp(m, 0);
        dp[0] = d[0][0];
        for(int i = 1; i < m; i++){
                dp[i] = dp[i - 1] + d[0][i];
        }

        for(int i = 1; i < n; i++){
                dp[0] += d[i][0];
                for(int j = 1; j < m; j++){
                        dp[j] = max(dp[j - 1], dp[j]) + d[i][j];
                }
        }

        return dp.back();
}

int main()
{
        int t = 0;
        cin >> t;

        while(t--){
                int n = 0, m = 0;
                cin >> n >> m;
                vector<vector<int>> d(n, vector<int>(m, 0));
                vector<vector<int>> rec(n, vector<int>(m, -1));
                for(int i = 0; i < n; i++){
                        for(int j = 0; j < m; j++){
                                cin >> d[i][j];
                        }
                }
                //cout << max_val(d, 0, 0, rec) << endl;
                //cout << max_dp(d) << endl;
                cout<<max_dp_space(d)<<endl;
        }
        return 0;
}